#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<iostream>
#include"Task.hpp"
#include"Tool.hpp"

int main()
{
    Tool tool;
    tool.PushFunc(DownLoad);
    tool.PushFunc(PrintLog);
    tool.PushFunc(FlushData);

    pid_t id = fork();
    if(id ==0 )
    {
        int cnt = 10;
        while(cnt--)
        {
            printf("我是子进程:%d, %d\n" , getpid() , cnt);
            sleep(1);
        }
        exit(0);
    }
    //父进程
    while(1)
    {
        pid_t rid = waitpid(id , nullptr , WNOHANG);
        if(rid > 0)
        {
            std::cout << "wait child success\n"<<std::endl;
            break;
        }
        else if(rid == 0)//waitpid 运行成功，但是子进程没有退出
        {
            std::cout << "child not quit\n";
            tool.Execute();
            sleep(1);
        }
        else
        {
            //rid 等于-1 意味着waitpid 执行异常
            std :: cout << "wait error\n";
        }
    }
    return 0;
}
